// import { useUserStore } from '@/stores/user'
import Cookies from 'js-cookie'
import { globalConfig } from '@vipl520/utils'

const TokenKey = 'access-token'

export function getPlaid() {
  return import.meta.env.VITE_BASE_PLAID || getQueryByName('plaid') || (window as any).dkSys?.plaid
}
//
// export function getAppid() {
//   return import.meta.env.VITE_BASE_APPID || getQueryByName('appid') || window.dkSys?.appid
// }

export function getToken(): any {
  return Cookies.get(TokenKey)
}

export function setToken(token: string) {
  return Cookies.set(TokenKey, token)
}

export function clearToken() {
  return Cookies.remove(TokenKey)
}

export function getQueryByNameHash(name: any) {
  const hashQuery = location.hash.split('?')[1]
  return hashQuery ? new URLSearchParams(hashQuery).get(name) : null
}

export function getQueryByName(name: any) {
  return (new URLSearchParams(location.search).get(name) || '').split('#')[0]
}

// export function auth(name: string | string[]) {
//   const userStore = useUserStore()
//
//   if (typeof name === 'string') {
//     return userStore.user.roles?.includes(name)
//   } else if (Array.isArray(name)) {
//     // Check if any role in the array is included in user roles
//     return name.some((role) => userStore.user.roles?.includes(role))
//   }
//   // Handle other cases, e.g., if name is neither string nor array
//   return false
// }

export function updateGlobalConfig() {
  globalConfig.ApiUrl = import.meta.env.VITE_BASE_URL
  globalConfig.AccessToken = getToken()
  // globalConfig.Plaid = getPlaid()
  globalConfig.AddonName = import.meta.env.VITE_BASE_ADDON_NAME
}

export function getDev() {
  return import.meta.env.DEV
}
